package com.gem.talkbase.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import com.gem.talkbase.R;
import com.gem.talkbase.bean.Firend;
import com.gem.talkbase.client.Client;
import com.gem.talkbase.fragment.Fragment_contact;
import com.gem.talkbase.fragment.Fragment_message;
import com.gem.talkbase.service.JSON_Friend_info;
import com.gem.talkbase.util.XmppTool;


import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 *
 */
public class Friend_infoActivity extends FragmentActivity {
	String requestPath=Client.Firend_info_URL;
	private List<Firend> list;
	ImageView friend_1,left_btn_5;
	TextView friend_nick,friend_ph2,friend_xb2,friend_qm2;
	Button send_bt;
	FinalBitmap fb ;
	String name;
	Context context; 
	String unameString,friendimage,friendID,username1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	 
		setContentView(R.layout.frined_info);
		Intent intent=getIntent();
	    username1 = intent.getStringExtra("u");//这是点击listView里面对应的昵称
	    name = intent.getStringExtra("name");//这才是登录者id号
	    System.out.println(username1+"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\===="+name);

		friend_1=(ImageView) findViewById(R.id.friend_1);
		left_btn_5=(ImageView)findViewById(R.id.left_btn_5);
		friend_nick=(TextView) findViewById(R.id.friend_nick);
		friend_ph2=(TextView) findViewById(R.id.friend_ph2);
		friend_xb2=(TextView) findViewById(R.id.friend_xb2);
		friend_qm2=(TextView) findViewById(R.id.friend_qm2);
		send_bt=(Button)findViewById(R.id.send_bt);
		if (username1==null) {
			Intent intent2=getIntent();
		    username1 = intent2.getStringExtra("yh_unick");
		    fun(username1);//传入好友昵称
		}else {
		  fun(username1);//传入好友昵称	
		}
		
		left_btn_5.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}	
		});
		
		send_bt.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				System.out.println("监听开始！");
				Intent intent=getIntent();
				String username = intent.getStringExtra("u");
			    name = intent.getStringExtra("name");	
				System.out.println(username+"+======"+name);
				//得到传递过来的好友名字
				  System.out.println("监听结束！");	
				 
				  intent = new Intent(Friend_infoActivity.this, Chat_Activity.class); 
				
	        	  //设置listView里面的名字，并且传送到好友信息界面
	        	  intent.putExtra("friendname",username1);//这是好友的昵称
	        	  intent.putExtra("friendimage", friendimage);//这是好友头像
	        	  intent.putExtra("friendid", friendID);//这是好友id
	        	  //传递登录的用户名
	        	  intent.putExtra("name", name);//传入登录者id号
	              System.out.println(friendID+"|||||==========||||||||"+name+"||||||||||||||||+++"+unameString);
				  startActivity(intent);
				  
				  Presence presence = new Presence(Presence.Type.available);
				  //创建服务，启动服务=========================================
				  XmppTool.getConnection().sendPacket(presence);
				  Intent service = new Intent(Friend_infoActivity.this,Chat_Activity.class);
				  startService(service);
				  finish();
		
				
			}	
		});
	}
	
	public void fun(final String username){
		AjaxParams params = new AjaxParams();
		params.put("username", username);
		FinalHttp fh = new FinalHttp();
		fh.post(requestPath, params, new AjaxCallBack<String>() {
			@Override
			public void onFailure(Throwable t, String strMsg) {
				System.out.println("加载失败！！！");
				super.onFailure(t, strMsg);
			}
			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				System.out.println("正在加载！！！");
				super.onLoading(count, current);
			}
			@TargetApi(Build.VERSION_CODES.HONEYCOMB)
			@Override
			public void onSuccess(String jsonString) {
	 			super.onSuccess(jsonString);

			 	fb = FinalBitmap.create(context);
				System.out.println(jsonString.toString()+"***********************");
				list=new ArrayList<Firend>();
				JSON_Friend_info json_Friend_info=new JSON_Friend_info();
				list= json_Friend_info.info(jsonString);
				Iterator<Firend> iterator=list.iterator();
				Firend firend = null;
				while(iterator.hasNext()){ 
					firend=iterator.next();
				    unameString=firend.getUsername();//这是id
				    friendimage=firend.getUimage();//这是头像
				    friendID=firend.getUsername();
					friend_nick.setText(username);//这也是昵称
					friend_ph2.setText(firend.getUsername());//这是好友id电话
					friend_xb2.setText(firend.getUxb());
					friend_qm2.setText(firend.getUstyle());
					fb.display(friend_1,firend.getUimage());
				} 
				
			}
		});
	}
}

